package com.zhuss.opus.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.vo.core.append.MessageVo;
import com.zhuss.bean.vo.opus.invit.DetailInvitAppendVo;
import com.zhuss.bean.vo.opus.invit.DetailInvitVo;
import com.zhuss.common.BasePageParam;
import com.zhuss.common.PageVo;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.enums.IsEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.opus.db.dao.ext.InvitAppendDao;


/**
 * 查询帖子Append processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("invitAppendRead")
public class InvitAppendRead {
	
	@Autowired
	private InvitAppendDao invitAppendDao;
	
	
	private void updateAlreadyRead(BasePageParam param, Integer appendType) {
		Long toUserId = param.getConcurrentUserId();
		invitAppendDao.updateAlreadyRead(appendType, toUserId, IsEnum.是.key());
	}
	
	public Integer countNoReadNum(Long toUserId, Integer appendType) {
		Integer noReadNum = invitAppendDao.countNoReadNum(appendType, toUserId, IsEnum.否.key());
		return noReadNum;
	}
	
	
	public MessageVo<DetailInvitAppendVo> listThumbBy(BasePageParam param) {
		MessageVo<DetailInvitAppendVo> result = new MessageVo<>();
		PageVo<DetailInvitAppendVo> pageVo = listBy(param, AppendTypeEnum.点赞.key());
		Integer noReadNum = countNoReadNum(param.getConcurrentUserId(), AppendTypeEnum.点赞.key());
		result.setPageVo(pageVo);
		result.setNoReadNum(noReadNum);
		updateAlreadyRead(param, AppendTypeEnum.点赞.key());
		return result;
	}
	
	private PageVo<DetailInvitAppendVo> listBy(BasePageParam param, Integer appendType) {
		Integer start = param.getStart();
		Integer length = param.getLength();
		Long toUserId = param.getConcurrentUserId();
		PageVo<DetailInvitAppendVo> result = new PageVo<DetailInvitAppendVo>();
		List<DetailInvitAppendVo> list = invitAppendDao.listAppend(appendType, toUserId, null, start, length);
		Integer total = invitAppendDao.countListAppend(appendType, toUserId, null);
		result.setItems(list);
		result.setPageNum(param.getPageNum());
		result.setPageSize(param.getPageSize());
		result.setTotal(total);
		return result;
	}
	
	private PageVo<DetailInvitAppendVo> listSelf(BasePageParam param, Integer appendType) {
		Integer start = param.getStart();
		Integer length = param.getLength();
		Long creatorId = param.getConcurrentUserId();
		PageVo<DetailInvitAppendVo> result = new PageVo<DetailInvitAppendVo>();
		List<DetailInvitAppendVo> list = invitAppendDao.listAppend(appendType, null, creatorId, start, length);
		Integer total = invitAppendDao.countListAppend(appendType, null, creatorId);
		result.setItems(list);
		result.setPageNum(param.getPageNum());
		result.setPageSize(param.getPageSize());
		result.setTotal(total);
		return result;
	}
	
	
	
	public PageVo<DetailInvitVo> listCollect2(BasePageParam param) {
		PageVo<DetailInvitAppendVo> pageVo = listSelf(param, AppendTypeEnum.收藏.key());
		PageVo<DetailInvitVo> result = new PageVo<>();
		result.setPageNum(pageVo.getPageNum());
		result.setPageSize(pageVo.getPageNum());
		result.setTotal(pageVo.getTotal());
		result.setTotalPage(pageVo.getTotalPage());
		List<DetailInvitAppendVo> itemList = pageVo.getItems();
		List<DetailInvitVo> itemList2 = new ArrayList<DetailInvitVo>();
		for (DetailInvitAppendVo item : itemList) {
			DetailInvitVo item2 = new DetailInvitVo();
			BeanUtils.copyProperties(item, item2);
			item2.setPic(item.getPic());
			itemList2.add(item2);
		}
		result.setItems(itemList2);
		return result;
	}
	
	public PageVo<DetailInvitAppendVo> listCollect(BasePageParam param) {
		PageVo<DetailInvitAppendVo> result = listSelf(param, AppendTypeEnum.收藏.key());
		return result;
	}
	
	public PageVo<DetailInvitAppendVo> listCollectBy(BasePageParam param) {
		PageVo<DetailInvitAppendVo> result = listBy(param, AppendTypeEnum.收藏.key());
		return result;
	}
	
	public PageVo<DetailInvitAppendVo> listForwardBy(BasePageParam param) {
		PageVo<DetailInvitAppendVo> result = listBy(param, AppendTypeEnum.转发.key());
		return result;
	}
	
	public PageVo<DetailInvitAppendVo> listRecommendBy(BasePageParam param) {
		PageVo<DetailInvitAppendVo> result = listBy(param, AppendTypeEnum.推荐.key());
		return result;
	}
	
	
	public List<DetailInvitAppendVo> listByType(Integer appendType, Long invitId, Long creatorId) {
		List<DetailInvitAppendVo> list = invitAppendDao.listByType(appendType, invitId, creatorId);
		return list;
	}
	
	public DetailInvitAppendVo getByType(Integer appendType, Long invitId, Long creatorId) {
		DetailInvitAppendVo result = invitAppendDao.getByType(appendType, invitId, creatorId);
		return result;
	}
	
	/**
	 * 帖子Append详情
	 * @param invitAppendId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long invitAppendId) throws WpsException {
		Object result = invitAppendDao.detail(invitAppendId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在, "invitAppendId: " + String.valueOf(invitAppendId));
		}
		return result;
	}
	
	

}
